package com.mrobuy.jdvc.dao.impl;

import com.gavel.database.SQLExecutor;
import com.mrobuy.jdvc.dao.VCPictureDao;
import com.mrobuy.jdvc.entity.VCPicture;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;

@Repository("vcPictureDao")
public class VCPictureDaoImpl implements VCPictureDao {


    private static Logger LOG = LoggerFactory.getLogger(VCPictureDao.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<VCPicture> query(String url) {
        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT * from VC_PICTURE where URL = ? ");
        try {
            List<VCPicture> list = jdbcTemplate.query(sql.toString(), new Object[]{url},  new BeanPropertyRowMapper(VCPicture.class));
            return list;
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return Collections.EMPTY_LIST;
    }

    @Override
    public VCPicture load(String url, String maskId) {
        try {
            return (VCPicture)jdbcTemplate.queryForObject("select * from VC_PICTURE where URL = ? and MASKID = ? ", new Object[]{url, maskId}, new BeanPropertyRowMapper(VCPicture.class));
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return null;
    }

    @Override
    public void insert(VCPicture vcPicture) {
        try {
            SQLExecutor.tempLateInsert(vcPicture, jdbcTemplate);
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }
    }

    @Override
    public void update(VCPicture vcPicture) {
        try {
            SQLExecutor.tempLateUpate(vcPicture, jdbcTemplate, false);
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }
    }
}
